import entity.Score;
import entity.Student;
import service.FileManager;
import service.ScoreAnalysis;
import service.ScoreManager;
import service.StudentManager;

import java.time.LocalDate;
import java.util.Map;

public class SystemTest {
    private static final ScoreManager scoreManager = new ScoreManager();
    private static final StudentManager studentManager = new StudentManager();
    private static final FileManager fileManager = new FileManager();

    public static void main(String[] args) {
        System.out.println("=== 开始自动化测试 ===\n");
        
        // 测试学生管理功能
        testStudentManagement();
        
        // 测试成绩管理功能
        testScoreManagement();
        
        // 测试统计分析功能
        testStatisticsAnalysis();
        
        // 测试数据持久化功能
        testDataPersistence();
        
        System.out.println("=== 自动化测试完成 ===");
    }
    
    private static void testStudentManagement() {
        System.out.println("--- 测试学生管理功能 ---");
        
        // 添加学生
        System.out.println("1. 添加学生测试：");
        Student student1 = new Student("202100001", "张三", 20, "计算机科学与技术", LocalDate.of(2021, 9, 1));
        Student student2 = new Student("202100002", "李四", 19, "软件工程", LocalDate.of(2021, 9, 1));
        Student student3 = new Student("202100003", "王五", 21, "信息安全", LocalDate.of(2021, 9, 1));
        
        studentManager.addStudent(student1);
        studentManager.addStudent(student2);
        studentManager.addStudent(student3);
        
        // 测试重复添加
        studentManager.addStudent(student1);
        
        // 显示所有学生
        System.out.println("2. 显示所有学生：");
        studentManager.showStudents();
        
        // 查询学生
        System.out.println("3. 查询学生测试：");
        Student student = studentManager.getStudentById("202100001");
        if (student != null) {
            System.out.println("查询到学生：" + student);
        }
        
        // 修改学生信息
        System.out.println("4. 修改学生信息测试：");
        Student updatedStudent = new Student("202100001", "张三丰", 22, "人工智能", LocalDate.of(2021, 9, 1));
        studentManager.updateStudent(updatedStudent);
        System.out.println("修改后的学生信息：");
        studentManager.showStudents();
        
        // 删除学生
        System.out.println("5. 删除学生测试：");
        studentManager.deleteStudent("202100003");
        System.out.println("删除后的学生列表：");
        studentManager.showStudents();
        
        System.out.println("--- 学生管理功能测试完成 ---\n");
    }
    
    private static void testScoreManagement() {
        System.out.println("--- 测试成绩管理功能 ---");
        
        // 添加成绩
        System.out.println("1. 添加成绩测试：");
        Score score1 = new Score("202100001", 85.5, 90.0, 88.5); // 总分264
        Score score2 = new Score("202100002", 78.0, 82.5, 80.0); // 总分240.5
        
        scoreManager.addScore(score1);
        scoreManager.addScore(score2);
        
        // 测试重复添加
        scoreManager.addScore(score1);
        
        // 显示所有成绩
        System.out.println("2. 显示所有成绩：");
        scoreManager.showScores();
        
        // 查询成绩
        System.out.println("3. 查询成绩测试：");
        Score score = scoreManager.getScoreById("202100001");
        if (score != null) {
            System.out.println("查询到成绩：" + score);
        }
        
        // 修改成绩
        System.out.println("4. 修改成绩测试：");
        Score updatedScore = new Score("202100001", 90.0, 92.0, 91.0); // 总分273
        scoreManager.updateScore(updatedScore);
        System.out.println("修改后的成绩：");
        scoreManager.showScores();
        
        // 删除成绩
        System.out.println("5. 删除成绩测试：");
        scoreManager.deleteScore("202100002");
        System.out.println("删除后的成绩列表：");
        scoreManager.showScores();
        
        System.out.println("--- 成绩管理功能测试完成 ---\n");
    }
    
    private static void testStatisticsAnalysis() {
        System.out.println("--- 测试统计分析功能 ---");
        
        // 计算平均分
        System.out.println("1. 计算平均分测试：");
        ScoreAnalysis scoreAnalysis = new ScoreAnalysis(scoreManager.getScores());
        System.out.printf("语文平均分：%.2f\n", scoreAnalysis.averageChinese());
        System.out.printf("数学平均分：%.2f\n", scoreAnalysis.averageMath());
        System.out.printf("英语平均分：%.2f\n", scoreAnalysis.averageEnglish());
        
        // 成绩排名
        System.out.println("2. 成绩排名测试：");
        scoreAnalysis.printScoresRanking(studentManager);
        
        // 成绩分布分析
        System.out.println("3. 成绩分布分析测试：");
        scoreAnalysis.analyzeScoreDistribution();
        
        System.out.println("--- 统计分析功能测试完成 ---\n");
    }
    
    private static void testDataPersistence() {
        System.out.println("--- 测试数据持久化功能 ---");
        
        // 保存学生数据
        System.out.println("1. 保存学生数据到文件：");
        fileManager.saveStudent(studentManager.getStudentMap());
        
        // 保存成绩数据
        System.out.println("2. 保存成绩数据到文件：");
        fileManager.saveScore(scoreManager.getScores());
        
        // 清空现有数据
        System.out.println("3. 清空现有数据：");
        studentManager.getStudentMap().clear();
        scoreManager.getScores().clear();
        System.out.println("清空后学生数量：" + studentManager.getStudentMap().size());
        System.out.println("清空后成绩数量：" + scoreManager.getScores().size());
        
        // 从文件加载学生数据
        System.out.println("4. 从文件加载学生数据：");
        Map<String, Student> loadedStudents = fileManager.loadStudents();
        System.out.println("加载的学生数量：" + loadedStudents.size());
        for (Student student : loadedStudents.values()) {
            System.out.println(student);
        }
        
        // 从文件加载成绩数据
        System.out.println("5. 从文件加载成绩数据：");
        Map<String, Score> loadedScores = fileManager.loadScores();
        System.out.println("加载的成绩数量：" + loadedScores.size());
        for (Score score : loadedScores.values()) {
            System.out.println(score);
        }
        
        // 备份数据
        System.out.println("6. 备份数据：");
        fileManager.saveBackup();
        
        System.out.println("--- 数据持久化功能测试完成 ---\n");
    }
}